package com.gable.exhibition.controller;

import com.gable.common.util.CacheData;
import com.gable.common.util.ReturnResultUtil;
import com.gable.common.util.StringUtil;
import com.gable.exhibition.model.ContractActualSend;
import com.gable.exhibition.service.ContractActualSendService;
import com.gable.exhibition.util.ExceptionUtil;
import com.gable.exhibition.util.MessageUtil;
import com.gable.exhibition.view.ParameterView;
import com.gable.exhibition.view.SendParameterView;
import com.gable.exhibition.view.UserView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by mj on 2018/2/8.
 * 合同实际发货
 */
@Controller
@RequestMapping(value = "/contractActualSend")
public class ContractActualSendController {

    @Autowired
    ContractActualSendService contractActualSendService;
    /**
     * 实际发货列表
     * @param request
     * @param parameterView
     * @return
     */
    @RequestMapping(value = "/actualSendList",method = RequestMethod.POST, produces="application/json;charset=utf-8")
    @ResponseBody
    public ReturnResultUtil actualSendList(HttpServletRequest request,ParameterView parameterView){
        ReturnResultUtil resultUtil = new ReturnResultUtil(500, MessageUtil.QUERYEXCEPTION);
        try {
            Long userId = parameterView.getUserId();
            if(userId == null)
                return new ReturnResultUtil(500,MessageUtil.USERIDDOESTNOEXIST);
            UserView sessionUser = CacheData.getSessionUser(userId, 1);
            if(sessionUser == null)
                return new ReturnResultUtil(600,MessageUtil.SESSIONNOTEXITIS);
            Long manufacturerId = sessionUser.getManufacturerId();  //厂商ID
            Long hospitalId = sessionUser.getHospitalId();      //医院ID
            Long supplierId = sessionUser.getSupplierId();      //供应商ID
            if(manufacturerId == null && hospitalId == null && supplierId == null)
                return new ReturnResultUtil(500,MessageUtil.ILLEGALUSER);

            parameterView.setManufacturerId(manufacturerId);
            parameterView.setHospitalId(hospitalId);
            parameterView.setSupplierId(supplierId);
            resultUtil = contractActualSendService.actualSendList(parameterView);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return resultUtil;
    }

    /**
     * 实际发货详情
     * @param request
     * @param parameterView
     * @return
     */
    @RequestMapping(value = "/actualSendInfo",method = RequestMethod.POST, produces="application/json;charset=utf-8")
    @ResponseBody
    public ReturnResultUtil actualSendInfo(HttpServletRequest request,ParameterView parameterView){
        ReturnResultUtil resultUtil = new ReturnResultUtil(500, MessageUtil.QUERYEXCEPTION);
        try {
            Long contractActualSendId = parameterView.getContractActualSendId();
            Long userId = parameterView.getUserId();
            if(userId == null)
                return new ReturnResultUtil(500,MessageUtil.USERIDDOESTNOEXIST);
            UserView sessionUser = CacheData.getSessionUser(userId, 1);
            if(sessionUser == null)
                return new ReturnResultUtil(600,MessageUtil.SESSIONNOTEXITIS);
            if(contractActualSendId == null)
                return new ReturnResultUtil(500,MessageUtil.CONTRACTESTIMATESENDID);
            Long manufacturerId = sessionUser.getManufacturerId();
            parameterView.setServiceId(manufacturerId);
            Long supplierId = sessionUser.getSupplierId();
            parameterView.setServiceId(supplierId);
            Long maintainerId = sessionUser.getMaintainerId();
            parameterView.setServiceId(maintainerId);

            resultUtil = contractActualSendService.actualSendInfo(parameterView);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return resultUtil;
    }

    //实际发货时选取合同列表
    @RequestMapping(value="/contractForActual",method = RequestMethod.POST, produces="application/json;charset=utf-8")
    @ResponseBody
    public ReturnResultUtil contractForActual(HttpServletRequest request, ParameterView parameterView){
        ReturnResultUtil resultUtil = new ReturnResultUtil(500, MessageUtil.QUERYEXCEPTION);
        try {
            Long userId = parameterView.getUserId();
            if(userId == null)
                return new ReturnResultUtil(500,MessageUtil.USERIDDOESTNOEXIST);
            UserView sessionUser = CacheData.getSessionUser(userId, 1);
            if(sessionUser == null)
                return new ReturnResultUtil(600,MessageUtil.SESSIONNOTEXITIS);
            Long manufacturerId = sessionUser.getManufacturerId();
            Long supplierId = sessionUser.getSupplierId();

            parameterView.setManufacturerId(manufacturerId);
            parameterView.setSupplierId(supplierId);

            resultUtil = contractActualSendService.contractForActual(parameterView);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return resultUtil;
    }

    //实际发货，发货
    @RequestMapping(value="/sendForActual",method = RequestMethod.POST, produces="application/json;charset=utf-8")
    @ResponseBody
    public ReturnResultUtil sendForActual(HttpServletRequest request, SendParameterView sendParameterView){
        ReturnResultUtil resultUtil = new ReturnResultUtil(500,MessageUtil.FAILED);
        try {
            Long userId = sendParameterView.getUserId();
            if(userId == null)
                return new ReturnResultUtil(500,MessageUtil.USERIDDOESTNOEXIST);

            UserView sessionUser = CacheData.getSessionUser(userId, 1);
            if(sessionUser == null)
                return new ReturnResultUtil(600,MessageUtil.SESSIONNOTEXITIS);
            Long manufacturerId = sessionUser.getManufacturerId();
            sendParameterView.setManufacturerId(manufacturerId);

            Long contractId = sendParameterView.getContractId();
            String sendDate = sendParameterView.getSendDate();
            String sendDeviceJson = sendParameterView.getSendDeviceJson();
            String sendNo = sendParameterView.getSendNo();
            String projectChargePerson = sendParameterView.getProjectChargePerson();//工程师名称
            String projectChargePersonPhone = sendParameterView.getProjectChargePersonPhone();//工程师电话
            String estimateInstallDate = sendParameterView.getEstimateInstallDate();//预计安装时间
            String hospitalContacts = sendParameterView.getHospitalContacts();
            String hospitalPhone = sendParameterView.getHospitalPhone();
            Long serviceType = sendParameterView.getServiceType();
            Integer oneself = sendParameterView.getOneself();
            Long serviceId = sendParameterView.getServiceId();

            if(contractId == null)
                return new ReturnResultUtil(500,MessageUtil.CONTRACTIDNOEXIST);
            if(StringUtil.isEmpty(sendDate))
                return new ReturnResultUtil(500,MessageUtil.SENDDATENOEXIST);
            if(StringUtil.isEmpty(sendDeviceJson) || "[]".equals(sendDeviceJson))
                return new ReturnResultUtil(500,MessageUtil.SENDDEVICEJSON);
            if(StringUtil.isEmpty(sendNo))
                return new ReturnResultUtil(500,MessageUtil.SENDNONOTNULL);
            if(StringUtil.isEmpty(hospitalContacts))
                return new ReturnResultUtil(500,MessageUtil.HOSPITALCONTACTS);
            if(StringUtil.isEmpty(hospitalPhone))
                return new ReturnResultUtil(500,MessageUtil.HOSPITALPHONE);
            if(serviceType == null)
                return new ReturnResultUtil(500,MessageUtil.SERVICETYPE);
            if(oneself == null)
                return new ReturnResultUtil(500,MessageUtil.ONESELFISNULL);
//            if(oneself == 1){
//                if(StringUtil.isEmpty(projectChargePerson))
//                    return new ReturnResultUtil(500,MessageUtil.PROJECTCHARGEPERSON);
//                if(StringUtil.isEmpty(projectChargePersonPhone))
//                    return new ReturnResultUtil(500,MessageUtil.PROJECTCHARGEPERSONPHONE);
//                if(StringUtil.isEmpty(estimateInstallDate))
//                    return new ReturnResultUtil(500,MessageUtil.ESTIMATEINSTALLDATE);
//            }
            if(oneself == 0 && serviceId == null)
                return new ReturnResultUtil(500,MessageUtil.SERVICEID);
            resultUtil = contractActualSendService.sendForActual(sendParameterView);
        }catch (ExceptionUtil eu){
            return new ReturnResultUtil(eu.getRet(),eu.getMsg(),eu.getData());
        }catch (Exception e) {
            e.printStackTrace();
        }
        return resultUtil;
    }

    //实际发货指派工程师
    @RequestMapping(value="/dispatchForSend",method = RequestMethod.POST, produces="application/json;charset=utf-8")
    @ResponseBody
    public ReturnResultUtil dispatchForSend(HttpServletRequest request, SendParameterView sendParameterView){
        ReturnResultUtil resultUtil = new ReturnResultUtil(500,MessageUtil.FAILED);
        try {
            Long userId = sendParameterView.getUserId();
            if(userId == null)
                return new ReturnResultUtil(500,MessageUtil.USERIDDOESTNOEXIST);

            UserView sessionUser = CacheData.getSessionUser(userId, 1);
            if(sessionUser == null)
                return new ReturnResultUtil(600,MessageUtil.SESSIONNOTEXITIS);
            Long manufacturerId = sessionUser.getManufacturerId();
            sendParameterView.setManufacturerId(manufacturerId);
            sendParameterView.setManufacturerName(sessionUser.getManufacturerName());

            Long serviceType = sendParameterView.getServiceType();
            Integer oneself = sendParameterView.getOneself();
            Long contractActualSendId = sendParameterView.getContractActualSendId();
            String hospitalContacts = sendParameterView.getHospitalContacts();
            String hospitalPhone = sendParameterView.getHospitalPhone();
            String estimateInstallDate = sendParameterView.getEstimateInstallDate();//预计安装时间
            String projectChargePerson = sendParameterView.getProjectChargePerson();//工程师名称
            String projectChargePersonPhone = sendParameterView.getProjectChargePersonPhone();//工程师电话
            Long serviceId = sendParameterView.getServiceId();
            if(serviceType == null)
                return new ReturnResultUtil(500,MessageUtil.SERVICETYPE);
            if(oneself == null)
                return new ReturnResultUtil(500,MessageUtil.ONESELFISNULL);
            if(contractActualSendId == null)
                return new ReturnResultUtil(500,MessageUtil.CONTRACTESTIMATESENDID);
            if(StringUtil.isEmpty(hospitalContacts))
                return new ReturnResultUtil(500,MessageUtil.HOSPITALCONTACTS);
            if(StringUtil.isEmpty(hospitalPhone))
                return new ReturnResultUtil(500,MessageUtil.HOSPITALPHONE);
            if(oneself == 1){
                if(StringUtil.isEmpty(projectChargePerson))
                    return new ReturnResultUtil(500,MessageUtil.PROJECTCHARGEPERSON);
                if(StringUtil.isEmpty(projectChargePersonPhone))
                    return new ReturnResultUtil(500,MessageUtil.PROJECTCHARGEPERSONPHONE);
                if(StringUtil.isEmpty(estimateInstallDate))
                    return new ReturnResultUtil(500,MessageUtil.ESTIMATEINSTALLDATE);
            }
            if(oneself == 0 && serviceId == null)
                return new ReturnResultUtil(500,MessageUtil.SERVICEID);
            resultUtil = contractActualSendService.dispatchForSend(sendParameterView);
        }catch (ExceptionUtil eu){
            return new ReturnResultUtil(eu.getRet(),eu.getMsg(),eu.getData());
        }catch (Exception e) {
            e.printStackTrace();
        }
        return resultUtil;
    }
}
